<template>
  <el-container>
    <el-header>
      <div>
        <img
          src="../assets/dy.jpg"
          alt=""
          width="200px"
        >
        <span>电商后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click='logout'
      >退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 侧边栏伸缩按钮 -->
        <div
          class='toggle-button'
          @click='isCollapse=!isCollapse'
        >|||</div>

        <!-- 侧边栏导航 -->
        <!-- router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
        <el-menu
          :default-active="$route.path"
          router
          :collapse-transition="false"
          :unique-opened="true"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
        >
          <!-- 一级菜单   -->
          <el-submenu
            :index="item.id+''"
            v-for=' item in menuList'
            :key='item.id'
          >
            <!-- 一级菜单模板  -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--   二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for='subItem in item.children'
              :key='subItem.id'
          
            >
              <!-- 二级菜单模板 -->
              <template>
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  props: [],
  //   页面以加载就要获取菜单列表
  created() {
    this.getMenuList()

  },
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      //   图标设置
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      //   控制折叠
      isCollapse: false,

    }
  },
  mounted () {

  },
  methods: {
    logout() {
      // 退出功能，清空token,跳转登录页
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 发送请求获取侧边菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) { return this.$message.error(res.meta.msg) }
      this.menuList = res.data
      // console.log(this.menuList[0])
    },


  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header{
  background-color:#373D41;
  color: #fff;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  >div{
     display: flex;
     align-items: center;
 
     span{
        margin-left: 15px;
     }
  }

}
// 解决有边框超出的一px
    .el-menu{
        border-right: 0;
    }
.el-aside{
  background-color:#333744;
}
.el-main{
  background-color:#eaedf1;
}
.iconfont{
    margin-right: 10px;
}
.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
